//
//  LYClassWebViewController.m
//  artist
//
//  Created by Jay_Apple on 16/5/16.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYClassWebViewController.h"
#import "WebViewJavascriptBridge.h"
#import "LYOrderDetailViewController.h"
#import "LYOrderDetailModel.h"
#import "LYCommentListViewController.h"
#import "LYTeacherWebViewController.h"
#import "LYChatFriendsModel.h"
#import "ChatViewController.h"
#import "LYCommitOrderViewController.h"
#import "LYOrzWebViewController.h"

#define classMainUrl  @"http://esj.zzzedu.cn/webview/view/course/course_details.php?"

@interface LYClassWebViewController ()<UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *webView;

@property (copy, nonatomic) NSString *urlStr;

@property (nonatomic, copy) NSString *ID;
@property WebViewJavascriptBridge *bridge;
@end


@implementation LYClassWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"课程详情";
    [self setUpWebView];
    [self buildBridge];

}

#pragma mark - 构造方法
+ (instancetype)classWebVCWithID:(NSString *) ID{
    
    LYClassWebViewController *webVC = [[self alloc] init];
    webVC.ID = ID;
    return webVC;
}

#pragma mark - 重写方法
- (void)setID:(NSString *)ID{
    
    _ID = [ID copy];
    
    NSString *parameter = [NSString stringWithFormat:@"course_id=%@&token=%@",self.ID,LYToken];
    self.urlStr = [classMainUrl stringByAppendingString:parameter];
    
}

#pragma mark - 初始化
/**
 *  初始化webView
 */
- (void)setUpWebView{
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //自适应屏幕
    self.webView.scalesPageToFit = YES;
    
    //加载请求
    if(self.urlStr){
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0];
        
        [self.webView loadRequest:request];
    }
}

/**
 *  桥接JS
 */
- (void)buildBridge{
    
    if (_bridge) { return; }
    
    __weak typeof(self) weakSelf = self;
    
    [WebViewJavascriptBridge enableLogging];
    
    _bridge = [WebViewJavascriptBridge bridgeForWebView:_webView webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) {
        
        
        
        NSLog(@"receivedMessage");
        
    }];
    
    //支付桥接
    [_bridge registerHandler:@"buyRightNow" handler:^(id data, WVJBResponseCallback responseCallback) {
        
        [[VVYCheckStatus sharedInstance] checkIsLoginBlock:^(LYUserInfoModel *model) {
           
            [weakSelf addOrder];
        }];
        
    }];
    
    //评价
    [_bridge registerHandler:@"startComment" handler:^(id data, WVJBResponseCallback responseCallback) {
        
        LYCommentListViewController *commentVC = [[LYCommentListViewController alloc] init];
        commentVC.data_id = weakSelf.ID;
        commentVC.type = CommentTypeClass;
        [weakSelf.navigationController pushViewController:commentVC animated:YES];
    }];
    
    //跳转机构或者老师
    [_bridge registerHandler:@"gotoTeacOrOrgzHome" handler:^(id data, WVJBResponseCallback responseCallback) {
        
        NSInteger type = [data[@"role_id"] integerValue];
        NSString *data_id = data[@"user_id"];
        
        switch (type) {
            case 1:{
                LYOrzWebViewController *orzVC = [LYOrzWebViewController orzWebVCWithID:data_id];
                [weakSelf.navigationController pushViewController:orzVC animated:YES];

                
            }
                break;
                
            case 2:{
                LYTeacherWebViewController *orzVC = [LYTeacherWebViewController teaWebVCWithID:data_id];
                [weakSelf.navigationController pushViewController:orzVC animated:YES];
                
            }
                break;
            default:
                break;
        }
        
    }];
    

    
}


#pragma mark - 网络请求
/**
 *  收藏
 */
- (void)collect{
    
    [MBProgressHUD showMessage:nil];
    [LYRequestTool collectionWithToken:LYToken type:ObjectTypeClass data_id:self.ID success:^{
        
        [MBProgressHUD hideHUD];
        [MBProgressHUD showSuccess:@"收藏成功"];
        
    } failure:^(NSString *code, NSString *message) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showOnlyText:message];
    }];
}

/**
 *  新增订单
 */
- (void)addOrder{

    LYCommitOrderViewController *commitVC = [[LYCommitOrderViewController alloc] init];
    commitVC.course_id = self.ID;
    [self.navigationController pushViewController:commitVC animated:YES];

}

#pragma mark - 按钮点击
/**
 *  收藏
 */
- (IBAction)collectBtnOnclick:(id)sender {
    
    [[VVYCheckStatus sharedInstance] checkIsLoginBlock:^(LYUserInfoModel *userInfo) {
        [self collect];
    }];
}


/**
 *  在线咨询
 */
- (IBAction)consultOnline:(id)sender {
    
    [[VVYCheckStatus sharedInstance] checkIsLoginBlock:^(LYUserInfoModel *userInfo) {
        WS(weakSelf);
        [_bridge callHandler:@"getInfo" data:nil responseCallback:^(id responseData) {
            LYChatFriendsModel *friend = [[LYChatFriendsModel alloc] init];
            friend.head_pic = responseData[@"head_pic"];
            friend.user_id = responseData[@"to_user_id"];
            friend.nick_name = responseData[@"nick_name"];
            friend.voip_account = responseData[@"voip_account"];
            
            ChatViewController *cvc = [[ChatViewController alloc] initWithSessionId:friend.voip_account];
            cvc.friendModel = friend;
            cvc.title = [[DemoGlobalClass sharedInstance] getOtherNameWithPhone:friend.voip_account];
            [weakSelf.navigationController pushViewController:cvc animated:YES];
            
        }];

        
        
    }];
    
}

/**
 *  电话咨询
 */
- (IBAction)callBtnOnclick:(id)sender {
    
    [[VVYCheckStatus sharedInstance] checkIsLoginBlock:^(LYUserInfoModel *userInfo) {
        
        [_bridge callHandler:@"getMobile" data:nil responseCallback:^(id responseData) {
            
            NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",responseData];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
            
        }];

        
    }];
    
}


#pragma mark - webView代理

- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    [MBProgressHUD showMessage:nil];
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
    [MBProgressHUD hideHUD];
    
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
    
    [MBProgressHUD hideHUD];
    [MBProgressHUD showError:@"网络不给力" toView:webView];
}


@end
